package com.liaoban.app.ui.mine;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.adapter.MFragmentPagerAdapter;
import com.liaoban.app.api.type.PayIndex;
import com.liaoban.app.api.type.event.PaySucEvent;
import com.liaoban.app.api.type.event.WxPayResultEvent;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.ConfigKey;
import com.liaoban.app.helper.ConfigHelper;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BasePayActivity;
import com.liaoban.app.util.CommonUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 充值
 */
public class RechargeActivity extends BasePayActivity {
    public static final int TYPE_ALI = 1;
    public static final int TYPE_WX = 2;

    @BindView(R.id.tv_yu)
    TextView tvYu;
    @BindView(R.id.tabs)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    public PayIndex.MoneyBean mAliMoneyBean; //阿里
    public PayIndex.MoneyBean mWxMoneyBean; //微信
    private int[] tabIcons = {
            R.drawable.ic_ali_pay,
            R.drawable.ic_wx_pay
    };
    private List<String> titles = new ArrayList<>();
    private MFragmentPagerAdapter mMFragmentPagerAdapter;

    @Override
    protected String getNewTitle() {
        return getString(R.string.recharge);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_recharge;
    }

    @Override
    public void paySuc() {
        showRunningDialog();
        startTask(CommonBiz.getInstance().payStatus(), payStatusBaseResponse -> {
            ToastHelper.show(RechargeActivity.this, payStatusBaseResponse.msg);
            ConfigHelper.save(this, ConfigKey.USER_LIAO_MONEY, String.valueOf(payStatusBaseResponse.data.getYuMoney()));
            tvYu.setText(String.valueOf(payStatusBaseResponse.data.getYuMoney()));
            EventBus.getDefault().post(new PaySucEvent());
        });
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setNeedOnBus(true);
        super.initView(savedInstanceState);
        loadData();
    }

    private void loadData() {
        showRunningDialog();
        startTask(CommonBiz.getInstance().payIndex(), payIndexBaseResponse -> {
            tvYu.setText(payIndexBaseResponse.data.getYuMoney() + "");
            initViewPager(payIndexBaseResponse.data.getMoney());
        });
    }

    private void initViewPager(List<PayIndex.MoneyBean> moneyBeans) {
        mMFragmentPagerAdapter = new MFragmentPagerAdapter(getSupportFragmentManager(), new Class[]{RechargeFragment.class, RechargeFragment.class}, new String[]{"支付宝", "微信支付"}, new Bundle[]{RechargeFragment.getBundle(TYPE_ALI, moneyBeans), RechargeFragment.getBundle(TYPE_WX, moneyBeans)});
        viewPager.setAdapter(mMFragmentPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        titles.add("支付宝");
        titles.add("微信支付");
        setupTabIcons();
        CommonUtils.setTabLayoutWidth(tabLayout, 44);
    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setCustomView(getTabView(0));
        tabLayout.getTabAt(1).setCustomView(getTabView(1));
    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_tab_recharge, null);
        TextView txt_title = view.findViewById(R.id.txt_title);
        txt_title.setText(titles.get(position));
        ImageView ivIcon = view.findViewById(R.id.ic_icon);
        ivIcon.setImageResource(tabIcons[position]);
        return view;
    }

    @OnClick(R.id.tv_pay)
    public void pay() {
        int curItem = viewPager.getCurrentItem();
        if (curItem == 0) {
            if (mAliMoneyBean == null) {
                ToastHelper.show(this, "请选择要充值的价格");
                return;
            }
            zfbPay(mAliMoneyBean);
        } else {
            if (mWxMoneyBean == null) {
                ToastHelper.show(this, "请选择要充值的价格");
                return;
            }
            wxPay(mWxMoneyBean);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onWxPayResultEvent(WxPayResultEvent wxPayResultEvent) {
        paySuc();
    }
}
